<template>
  <div class="home">
    <el-container>
      <el-aside width="auto">
        <!-- router表示开启路由模式，它会以index作为路径进行跳转 -->
        <!-- collapse属性通过布尔值控制菜单的展开和折叠 -->
        <el-menu
          :collapse="iscollapse"
          class="el-menu-admin"
          router
          :default-active="$route.name"
          @open="handleOpen"
          @close="handleClose">
          <div class="logo"></div>
          <el-submenu :index="item.path" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{item.authName}}</span>
            </template>
          <el-menu-item :index="submenu.path" v-for="submenu in item.children" :key="submenu.id">
            <i class="el-icon-setting"></i>
            <span slot="title">{{submenu.authName}}</span>
          </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-container>
        <el-header>
          <div class="myicon myicon-menu toggle-btn" @click="toggle"></div>
          <div class="system-title">电商后台管理系统</div>
          <div>
            <span class="welcome">您好：{{uname}}</span>
            <el-button type="warning" round @click="logout">退出</el-button>
          </div>
        </el-header>
        <el-main>
          <!-- 首先将router-view用transition组件包裹起来
          此处必须加上mode属性，如果不加，两个组件在切换是会同时有动画 -->
          <transition
             mode="out-in"
             enter-active-class="animated fadeIn"
             leave-active-class="animated fadeOut"
          >
            <router-view></router-view>
          </transition>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { getMenus } from '@/api'
export default {
  data () {
    return {
      iscollapse: false, // 控制菜单的展开和折叠，默认false为展开
      uname: '',
      menuList: []
      // activeMenu: 'welcome' // 默认激活的菜单为welcome
    }
  },
  mounted () {
    // 获取本地中保存的用户名
    let username = localStorage.getItem('username')
    this.uname = username
    this.initMenus()

    // 组件挂载的时候，将$route中的name属性取出来，赋值给activeMenu属性
    this.activeMenu = this.$route.name
  },
  methods: {
    toggle () {
      // 点击时改变iscollapse的true或false
      this.iscollapse = !this.iscollapse
    },
    // 退出登录
    logout () {
      // 跳转到登录页，清除本地存储
      localStorage.clear()
      this.$router.push({ name: 'login' })
    },
    // 初始化左侧菜单
    initMenus () {
      getMenus()
        .then(res => {
          this.menuList = res.data.data
        })
    },
    handleOpen (key, keyPath) {
      console.log(key, keyPath)
    },
    handleClose (key, keyPath) {
      console.log(key, keyPath)
    }
  }

}
</script>

<style lang="scss" scoped>
.home {
  height: 100%;
  .el-menu-admin:not(.el-menu--collapse) {
    width: 200px;
    min-height: 100%;
  }
  .el-container {
    height: 100%;
  }
  .el-aside {
    background-color: #F9F9F9;
  }
  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #409EFF;
  }
  .logo {
    height:60px;
    background: url(../assets/assets/logo.png);
    background-size: cover;
    background-color: white;
  }
  .toggle-btn {
    padding: 0 15px;
    margin-left: -20px;
    font-size: 36px;
    color: white;
    cursor: pointer;
    line-height: 60px;
    &:hover {
      background-color: #4292CF;
    }
  }
  .system-title {
    font-size: 28px;
    color: white;
  }
  .welcome, {
    color: white;
  }
}
</style>
